//
//  NSString+Weekday.m
//  lifestyle
//
//  Created by wd on 15/8/3.
//  Copyright (c) 2015年 Wei Chuang Le ,Ltd. All rights reserved.
//

#import "NSString+Weekday.h"

@implementation NSString (Weekday)

/**
 *  时间 date 转星期
 *
 *  @param inputDate nsdata
 *
 *  @return week (明天)
 */
+ (NSString *)weekdayStringFromDate:(NSDate*)inputDate
{
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", @"今天", @"明天", @"后天",nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    NSInteger index = theComponents.weekday;
    
    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT];
    NSDate* todayDate = [NSDate dateWithTimeIntervalSinceNow:0];
    [todayDate dateByAddingTimeInterval:timeZoneOffset];
    NSDateComponents *todayComponents = [calendar components:calendarUnit fromDate:todayDate];
    
    if (theComponents.weekday == todayComponents.weekday) {
        index = 8;
    } else if (theComponents.weekday == todayComponents.weekday%7+1) {
        index = 9;
    }  else if (theComponents.weekday == todayComponents.weekday%7+2) {
        index = 10;
    }
    
    //    NSLog(@"weekday = %ld",(long)theComponents.weekday);
    
    return [weekdays objectAtIndex:index];
}

+ (NSString *)timeDifferFromPreDate:(NSDate *)preDate toLaterDate:(NSDate *)latDate
{
    NSTimeInterval time = [preDate timeIntervalSinceDate:latDate];
    
    BOOL greater = NO;
    
    if (time < 0) {
        time = 0 - time;
    } else {
        
        NSDate *tempDate = latDate;
        latDate = preDate;
        preDate = tempDate;
        
        greater = YES;
    }
    
    NSString *beforeOrAfter = greater ? @"后" : @"前";
    
    int month = (int)(latDate.month - preDate.month);
    int year = (int)(latDate.year - preDate.year);
    int day = (int)(latDate.day - preDate.day);
    
    NSTimeInterval retTime = 1.0;
    
    // 小于一小时
    if (time < 3600) {
        retTime = time / 60;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"%.0f*分钟%@", retTime, beforeOrAfter];
    }
    
    // 小于一天，也就是今天
    else if (time < 3600 * 24) {
        retTime = time / 3600;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"%.0f*小时%@", retTime, beforeOrAfter];
    }
    
    // 昨天
    else if (time < 3600 * 24 * 2) {
        return [NSString stringWithFormat:@"1*天%@", beforeOrAfter];
    }
    
    // 第一个条件是同年，且相隔时间在一个月内
    // 第二个条件是隔年，对于隔年，只能是去年12月与今年1月这种情况
    else if ((abs(year) == 0 && abs(month) <= 1) ||
             (abs(year) == 1 && latDate.month == 1 && preDate.month == 12)) {
        
        int retDay = 0;
        
        // 同年
        if (year == 0) {
            // 同月
            if (month == 0) {
                retDay = day;
            }
        }
        
        if (retDay <= 0) {
            // 这里按月最大值来计算
            // 获取发布日期中，该月总共有多少天
            NSInteger totalDays = preDate.daysCountForYearMonth;
            // 当前天数 + （发布日期月中的总天数-发布日期月中发布日，即等于距离今天的天数）
            retDay = (int)(latDate.day + (totalDays - preDate.day));
            
            if (retDay >= totalDays) {
                return [NSString stringWithFormat:@"%d*个月%@", (abs)(MAX(retDay / 31, 1)), beforeOrAfter];
            }
        }
        
        return [NSString stringWithFormat:@"%d*天%@", (abs)(retDay), beforeOrAfter];
        
    } else  {
        
        if (abs(year) <= 1) {
            
            if (year == 0) { // 同年
                return [NSString stringWithFormat:@"%d*个月%@", abs(month), beforeOrAfter];
            }
            
            // 相差一年
            // 隔年，但同月，就作为满一年来计算
            if (latDate.month == 12 && preDate.month == 12) {
                return [NSString stringWithFormat:@"1*年%@", beforeOrAfter];
            }
            
            // 相隔不满2年
            return [NSString stringWithFormat:@"%d*个月%@", (abs)(12 - (int)preDate.month + (int)latDate.month), beforeOrAfter];
        }
        
        // 相隔2年以上
        return [NSString stringWithFormat:@"%d*年%@", abs(year), beforeOrAfter];
    }
    
    return [NSString stringWithFormat:@"1*小时%@", beforeOrAfter];
}

/**
 *  前者某一时间 距离 后者某一时间 多久
 *
 *  @param preDateString 先前某一时间 @"yyyy-MM-dd-HH-mm-ss"
 *  @param latDateString 后来某一时间 @"yyyy-MM-dd-HH-mm-ss"
 *
 *  @return x分钟前/x小时前/x天前/x个月前/x年前 或者 x分钟后/x小时后/x天后/x个月后/x年后
 */
+ (NSString *)timeDifferFromPreDateString:(NSString *)preDateString toLaterDate:(NSString *)latDateString
{
    // 把日期字符串格式化为日期对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    
    NSDate *preDate = [dateFormatter dateFromString:preDateString];
    
    NSDate *latDate = [dateFormatter dateFromString:latDateString];
    
    return [NSString timeDifferFromPreDate:preDate toLaterDate:latDate];
}

/**
 *  某一时间 距离现在多久
 *
 *  @param dateString 过去某一时间点 @"yyyy-MM-dd-HH-mm-ss"
 *
 *  @return x分钟前/x小时前/x天前/x个月前/x年前 或者 x分钟后/x小时后/x天后/x个月后/x年后
 */
+ (NSString *)timeInfoWithDateString:(NSString *)dateString
{
    // 把日期字符串格式化为日期对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    NSDate *curDate = [NSDate date];
    
    return [NSString timeDifferFromPreDate:date toLaterDate:curDate];
    
}

/**
 *  距离某一时间 相差（早于） 天数  (self = @"yyyy-MM-dd-HH-mm-ss")
 *
 *  @param toDateString 某一时间  (toDateString = @"yyyy-MM-dd-HH-mm-ss")
 *  @param distanceDays 相差（早于） 天数
 *
 *  @return 是否相差（早于） ？天数 （注：YES 早于 NO：不早于 或晚于）
 */
- (BOOL)timeToTheDate:(NSString *)toDateString distanceDays:(NSInteger)distanceDays
{
    BOOL laterDay = NO;
    
    // 把日期字符串格式化为日期对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    
    NSDate *preDate = [dateFormatter dateFromString:self];
    NSDate *latDate = [dateFormatter dateFromString:toDateString];

    NSTimeInterval time = [latDate timeIntervalSinceDate:preDate];
    
    if (time < 0) {
    
        return NO;
    
    } else {
    
        if (time >= 3600 * 24 * distanceDays) {
            
            laterDay = YES;
            
        } else {
            
            laterDay = NO;
        }
    }
    
    return laterDay;
}

@end
